package com.why.commonresults;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回结果
 */

@Data
public class Result {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<String, Object>();

    //构造方法私有化，此时只能使用我们规定的构造方法！
    private Result() {
    }

    //两个静态构造方法，掉用下面这两个构造方法，可以创建对应的响应结果集。
    public static Result ok() {
        Result r = new Result();
        r.setSuccess(true);
        r.setCode(RESULTCODE.SUCCESS.getCode());
        r.setMessage(RESULTCODE.SUCCESS.getMessage());
        return r;
    }

    public static Result error() {
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(RESULTCODE.ERR.getCode());
        r.setMessage(RESULTCODE.ERR.getMessage());
        return r;
    }

    /**
     * 进行响应的各种信息设置，最终返回当前对象
     * @param success
     * @return
     */
    public Result success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public Result message(String message) {
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }

    /**
     * 设置响应数据
     * @param key
     * @param value
     * @return
     */
    public Result data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public Result data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
}